<?php

namespace App\JsonRpc;

use App\Constant\Code;
use App\Exception\LogicException;
use App\Rpc\UserLoginServiceInterface;
use Hyperf\RpcServer\Annotation\RpcService;

/**
 * @RpcService(name="user.login", protocol="jsonrpc", server="jsonrpc")
 */
class UserLoginService extends JsonRpcService implements UserLoginServiceInterface
{

    /**
     * 手机号码检测
     * @param string $phone
     * @return int
     */
    public function phoneLoginCheck(string $phone): int
    {
        //没有注册
        return 0;
    }


    /**
     * 发送手机验证码
     * @param string $phone
     * @return bool
     */
    public function sendLoginPhoneCode(string $phone): bool
    {
        $code = "123456";

        if(env("APP_ENV") == "prod" ){
            //todo send sms
        }
        return true;
    }

    /**
     * 短信验证码登录
     * @param string $phone
     * @param string $code
     * @return array
     * @throws LogicException
     */
    public function loginWithPhoneCode(string $phone, string $code): array
    {
        //TODO 检查短信验证码，根据手机号查找用户
        if(rand(0,1)){
            throw new LogicException("错误的手机验证码!",Code::INVALID_PHONE_CODE,);
        }
        return [
            "user_id" => rand(111111111,9999999999),
            "user_name" => sprintf("user-%s",uniqid()),
        ];
    }
}